package com.tms.app.tms.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.ldoublem.loadingviewlib.view.LVCircularRing;
import com.tms.app.tms.R;

/**
 * Created by hl on 2017/6/22.
 */

public class CommonDialog {
    public  static final int btnOne=1;
    public  static final int btnTwo=2;
    public  static final int btnThree=3;
    private  View.OnClickListener myLinser=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mLoadingDialog!=null){
               close();
            }
        }
    };
    Dialog mLoadingDialog;

    public static CommonDialog getInstance(Context context, View.OnClickListener listener,
                                           String text21) {
        return getInstance(context,listener,btnTwo,text21);
    }
    public static CommonDialog getInstance(Context context, View.OnClickListener listener, int btnType,
                                           String text21) {
       return  new CommonDialog(context,listener,btnType,
                "","","",
                true,true,false,
                "","",
                text21,"","",
                "","","");
    }
    public static CommonDialog getInstance(Context context, View.OnClickListener listener, int btnType,
                                           String btnText1,String btnText2, String btnText3,
                                           boolean layout1,boolean layout2,boolean layout3,
                                           String text12,String text13,
                                           String text21,String text22,String text23,
                                           String text31,String text32,String text33){
        return  new CommonDialog(context,listener,btnType,
                btnText1,btnText2,btnText3,
                layout1,layout2,layout3,
                text12,text13,
                text21,text22,text23,
                text31,text32,text33);
    }

    private CommonDialog(Context context, View.OnClickListener listener, int btnType,
                        String btnText1,String btnText2, String btnText3,
                        boolean layout1,boolean layout2,boolean layout3,
                        String text12,String text13,
                        String text21,String text22,String text23,
                        String text31,String text32,String text33) {
        if (mLoadingDialog!=null&&mLoadingDialog.isShowing())
            return;
        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate( R.layout.layout_dialog_net_errot, null);
        // 获取整个布局
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
        TextView textView1_1=view.findViewById(R.id.text_1_1);
        TextView textView1_2=view.findViewById(R.id.text_1_2);
        TextView textView1_3=view.findViewById(R.id.text_1_3);

        TextView textView2_1=view.findViewById(R.id.text_2_1);
        TextView textView2_2=view.findViewById(R.id.text_2_2);
        TextView textView2_3=view.findViewById(R.id.text_2_3);

        TextView textView3_1=view.findViewById(R.id.text_3_1);
        TextView textView3_2=view.findViewById(R.id.text_3_2);
        TextView textView3_3=view.findViewById(R.id.text_3_3);


        TextView button1=view.findViewById(R.id.dialog_photo_button_1);
        TextView button2=view.findViewById(R.id.dialog_photo_button_2);
        TextView button3=view.findViewById(R.id.dialog_photo_button_3);

        textView1_1.setOnClickListener(listener);
        textView1_2.setOnClickListener(listener);
        textView1_3.setOnClickListener(listener);
        textView2_1.setOnClickListener(listener);
        textView2_2.setOnClickListener(listener);
        textView2_3.setOnClickListener(listener);
        textView3_1.setOnClickListener(listener);
        textView3_2.setOnClickListener(listener);
        textView3_3.setOnClickListener(listener);

        //textView1_1.setText(text11);
        textView1_2.setText(text12);
        textView1_3.setText(text13);
        textView2_1.setText(text21);
        textView2_2.setText(text22);
        textView2_3.setText(text23);
        textView3_1.setText(text31);
        textView3_2.setText(text32);
        textView3_3.setText(text33);

        if (!layout1){
            view.findViewById(R.id.layout_1).setVisibility(View.GONE);
        }
        if (!layout2){
            view.findViewById(R.id.layout_2).setVisibility(View.GONE);
        }
        if (!layout3){
            view.findViewById(R.id.layout_3).setVisibility(View.GONE);
        }

        button1.setOnClickListener(myLinser);
        button2.setOnClickListener(myLinser);
        button3.setOnClickListener(myLinser);
        if (listener!=null){
            button2.setOnClickListener(listener);
            button3.setOnClickListener(listener);
        }

        if (!StringUtil.isEmpty(btnText1))
            button1.setText(btnText1);
        if (!StringUtil.isEmpty(btnText2))
            button2.setText(btnText2);
        if (!StringUtil.isEmpty(btnText3))
            button3.setText(btnText3);
        switch (btnType)
        {
            case btnOne:
                //button1.setVisibility(View.INVISIBLE);
                button2.setVisibility(View.GONE);
                button3.setVisibility(View.GONE);
                break;
            case btnTwo:
                //button1.setVisibility(View.INVISIBLE);
                //button2.setVisibility(View.INVISIBLE);
                button3.setVisibility(View.GONE);
                break;
            case btnThree:
                //button1.setVisibility(View.INVISIBLE);
                //button2.setVisibility(View.INVISIBLE);
                //button3.setVisibility(View.INVISIBLE);
                break;
        }




        // 创建自定义样式的Dialog
     //   mLoadingDialog = new Dialog(context, R.style.loading_dialog);

        mLoadingDialog = new Dialog(context);
        // 设置返回键无效
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(true);
        mLoadingDialog.setContentView(view);
    }



    public void show(){
        if (mLoadingDialog!=null&&mLoadingDialog.isShowing())
            return;
        mLoadingDialog.show();
    }

    public boolean isShowing(){
        if (mLoadingDialog==null)
            return false;
        return mLoadingDialog.isShowing();
    }

    public void close(){
        if (mLoadingDialog!=null) {
            if (isShowing())
                mLoadingDialog.dismiss();
            mLoadingDialog=null;
        }
    }
}
